<?php

namespace App\Http\Resources;

use App\model\Workshop;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Facades\Storage;

class WorkShopCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $image=[
            '19'=>'fanyu.png',
            '1'=>'nansha.png',
            '8'=>'shunde.png',
        ];
        return [
            'code'=>0,
            'data'=>$this->collection->map(function($item)use($image){
               if($item->type==0){
                   $price=$item->rent!=0 ? $item->rent.Workshop::RENT  : '面议';
               }else{
                   $price=$item->price!=0 ? $item->rent.Workshop::SALE  : '面议';
               }
               $qu_id=$item->qu->id ?? '';
                return [
                    'workshop_id'=>$item->id,
                    'logo'=>$item->logo.Workshop::LOGO,
                    'city_image'=>$qu_id ? $image[$item->qu->id] : '',
                    'title'=>$item->title,
                    'industry'=>$item->industry->reject(function($item,$key){
                        return $key>1;
                    }),
                    'area'=>$item->area,
                    'park_name'=>$item->park->title ?? '',
                    'price'=>$price,
                    'tags'=>$item->tags->reject(function($item,$key){
                        return $key>2;
                    }),
                    'is_zhiding'=>$item->is_zhiding,
                    'qu'=>$item->qu->title ?? '',
                    'shenhe'=>$item->shenhe,
                    'overdue'=> \Illuminate\Support\Carbon::parse($item->commission_end_day)->lt(Carbon::now()->addDays(30)) ? 1 :0,
                        //todu
                     'is_new'=>Carbon::parse($item->updated_at)->addDays(5)->gt(Carbon::now()) ? 1 : 0, //5天前为最新
                    'times'=>substr($item->updated_at,0,10),
                ];
            })
        ];
    }
}
